<template>
  <!-- 主体内容容器 -->
  <div class="wrapper">
    <!-- 主体商铺展示内容 -->
    <div class="title">我的订单</div>
    <div class="orders">
      <div class="order" v-for="(item, index) in list" :key="index">
        <div class="order__title">
          {{ item.shopName }}
          <span class="order__status">{{ item.isCanceled ? '已取消' : '已下单' }}</span>
        </div>
        <div class="order__content">
          <span class="order__content__imgs">
            <template v-for="(itemImg, indexImg) in item.products" :key="indexImg">
              <img
                class="order__content__img"
                :src="itemImg.product.imgUrl"
                alt="图片"
                v-if="indexImg <= 3"
              />
            </template>
          </span>
          <div class="order__content__info">
            <div class="order__content__info__price">&yen;{{ item.totalPrice }}</div>
            <div class="order__content__info__count">共{{ item.totalNumber }}件</div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- 底部主菜单容器 -->
  <Docker :current-index="2" />
  <Toast v-if="show" :message="message" />
  <router-view />
</template>

<script>
  import { reactive, toRefs } from 'vue'
  import { get } from '@/utils/request.js'
  import Toast, { useToastEffect } from '@/components/Toast/Toast'

  const useOrderListEffect = toastMsg => {
    const data = reactive({ list: [] })
    const getNearbyList = async () => {
      // 可以写成： const resultData = await get(`/api/shop/${route.params.id}`)
      const resultData = await get('/api/order/')
      if (resultData?.code === 200 && resultData?.data?.length) {
        console.log('data :' + JSON.stringify(data))
        const tempData = resultData.data || []
        tempData.forEach(item => {
          const products = item.products
          let totalPrice = 0 //商品总价
          let totalNumber = 0
          products.forEach(item2 => {
            totalNumber += Number(item2?.orderSale || 0)
            totalPrice += Number(item2?.product.price || 0) * Number(item2?.orderSales || 0)
          })
          item.totalPrice = totalPrice.toFixed(2)
          item.totalNumber = totalNumber
        })
        data.list = tempData
      } else {
        toastMsg('没有数据！')
      }
    }
    getNearbyList()
    const { list } = toRefs(data)
    return { getNearbyList, list }
  }

  import Docker from '@/components/Docker/Docker'
  export default {
    name: 'OrderList',
    components: { Docker, Toast },
    setup() {
      const { show, message, toastMsg } = useToastEffect()
      const { list } = useOrderListEffect(toastMsg)

      return { show, message, list }
    }
  }
</script>

<style lang="scss" scoped>
  @import '@/style/viriables.scss';
  .wrapper {
    overflow-y: auto;
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0.5rem;
    right: 0;
    background-color: #f8f8f8;
  }
  .title {
    line-height: 0.44rem;
    background: $bg-color;
    font-size: 0.16rem;
    color: $content-font-color;
    text-align: center;
  }

  .orders {
  }

  .order {
    padding: 0.16rem;
    background: $bg-color;
    margin: 0.16rem 0.18rem;
    line-height: 0.22rem;

    &__title {
      margin-bottom: 0.16rem;
      font-size: 0.16rem;
      color: $content-font-color;
    }
    &__status {
      float: right;
      font-size: 0.14rem;
      color: $light-font-color;
    }
    &__content {
      display: flex;
      &__imgs {
        flex: 1;
      }
      &__img {
        width: 0.4rem;
        height: 0.4rem;
        margin-right: 0.12rem;
      }
      &__info {
        width: 0.7rem;
        &__price {
          line-height: 0.2rem;
          margin-bottom: 0.04rem;
          font-size: 0.14rem;
          color: $height-light-font-color;
          text-align: right;
        }
        &__count {
          line-height: 0.14rem;
          font-size: 0.12rem;
          color: $content-font-color;
          text-align: right;
        }
      }
    }
  }
</style>
